<template>
  <el-dialog title="分配权限" :visible.sync="isOpen">
    <el-form :model="form">
      <p>
        <span>当前用户: </span><el-tag>{{ currentName }}</el-tag>
      </p>
      <p>
        <span>当前角色: </span><el-tag>{{ currentRole }}</el-tag>
      </p>
      <el-form-item label="分配角色" label-width="120px">
        <el-select v-model="form.region" placeholder="请选择活动区域">
          <el-option
            v-for="item of roles"
            :key="item.id"
            :label="item.roleName"
            :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="isOpen = false;form.region = '超级管理员'">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script lang="ts">

import { Component, Vue } from 'vue-property-decorator'
import { updateUserRole } from '@/api/user'

@Component
export default class AssignUser extends Vue {
  public form = {
    region: '超级管理员'
  }

  mounted () {
    //
  }

  public get roles () {
    const data = this.$store.state.user.roles
    console.log(data)
    return data
  }

  public currentName = ''
  public currentRole = ''
  public currentId =1
  public isOpen = false

  async submit () {
    console.log(this.form)
    if (typeof this.form.region !== 'number') return
    const { meta } = await updateUserRole(this.currentId, this.form.region)
    this.open(meta.msg, meta.status === 400 ? 'error' : 'success')
    this.isOpen = false
    this.form.region = '超级管理员'
  }

  open (
    smg = '登陆成功',
    type: 'success' | 'warning' | 'info' | 'error' = 'success'
  ) {
    this.$message({
      message: smg,
      type: type
    })
  }
}
</script>

<style lang="less" scoped>
.el-dialog{
  p{
    margin-bottom: 5px;
    span:nth-child(1){
      margin-left: 50px;
    }
    span:nth-child(2){
      margin-left: 10px;
    }
  }
}
</style>
